package com.pandas.community.service;

import com.pandas.community.Utils.SensitiveFilter;
import com.pandas.community.dao.DiscussPostMapper;
import com.pandas.community.entity.DiscussPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.Date;
import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<DiscussPost> getDiscussPost(int userid, int offset, int limit){
        return discussPostMapper.selectDiscussPost(userid, offset, limit);
    }

    public int getDisscussPostAmount(int userid){
        return discussPostMapper.selectDiscussPostAmount(userid);
    }

    public int addDiscussPost(DiscussPost post){
        if(post == null) throw new IllegalArgumentException("添加帖子参数为空！");

        // 转义
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));

        //过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return discussPostMapper.addDiscussPost(post);
    }

    public DiscussPost selectDiscussPostById(int id){
        return discussPostMapper.selectDiscussPostById(id);
    }

    public int updateCommentCount(int id, int commentCount){
        return discussPostMapper.updateCommentCount(id, commentCount);
    }
}
